﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ESoft.Helpers
{

    /// <summary>
    ///     转换类
    /// </summary>
    public class ConvertHelper
    {

        #region 数值转换

        #region ToByte

        /// <summary>
        ///     转换为byte
        /// </summary>
        /// <param name="data">数据</param>
        public static byte ToByte(object data)
        {
            if (data == null)
                return 0;

            var success = byte.TryParse(data.ToString(), out var result);
            if (success)
                return result;

            try
            {
                return Convert.ToByte(ToDouble(data, 0));
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        ///     转换为可空byte
        /// </summary>
        /// <param name="data">数据</param>
        public static byte? ToByteOrNull(object data)
        {
            if (data == null)
                return null;

            var isValid = byte.TryParse(data.ToString(), out var result);
            if (isValid)
                return result;

            return null;
        }

        #endregion ToByte

        #region ToInt

        /// <summary>
        ///     转换为整型
        /// </summary>
        /// <param name="data">数据</param>
        public static int ToInt(object data)
        {
            if (data == null)
                return 0;

            var success = int.TryParse(data.ToString(), out var result);
            if (success)
                return result;

            try
            {
                return Convert.ToInt32(ToDouble(data, 0));
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        ///     转换为整型，小于0返回0，否则返回原int值
        /// </summary>
        /// <param name="data">数据</param>
        public static int ToInt0(object data)
        {
            var value = ToInt(data);
            return value < 0 ? 0 : value;
        }

        /// <summary>
        ///     转换为整型，小于0返回0，否则返回原int值
        /// </summary>
        /// <param name="data">数据</param>
        public static int ToInt1(object data)
        {
            var value = ToInt(data);
            return value < 1 ? 1 : value;
        }

        /// <summary>
        ///     转换为可空整型
        /// </summary>
        /// <param name="data">数据</param>
        public static int? ToIntOrNull(object data)
        {
            if (data == null)
                return null;

            var isValid = int.TryParse(data.ToString(), out var result);
            if (isValid)
                return result;

            return null;
        }

        #endregion ToInt

        #region ToLong

        /// <summary>
        ///     转换为long
        /// </summary>
        /// <param name="data">数据</param>
        public static long ToLong(object data)
        {
            if (data == null)
                return 0;

            var success = long.TryParse(data.ToString(), out var result);
            if (success)
                return result;

            try
            {
                return Convert.ToInt64(ToDouble(data, 0));
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        ///     转换为long，小于0返回0，否则返回原int值
        /// </summary>
        /// <param name="data">数据</param>
        public static long ToLong0(object data)
        {
            var value = ToLong(data);
            return value < 0 ? 0 : value;
        }

        /// <summary>
        ///     转换为long，小于1返回1，否则返回原int值
        /// </summary>
        /// <param name="data">数据</param>
        public static long ToLong1(object data)
        {
            var value = ToLong(data);
            return value < 1 ? 1 : value;
        }

        /// <summary>
        ///     转换为可空long
        /// </summary>
        /// <param name="data">数据</param>
        public static long? ToLongOrNull(object data)
        {
            if (data == null)
                return null;

            var isValid = long.TryParse(data.ToString(), out var result);
            if (isValid)
                return result;

            return null;
        }

        #endregion ToLong

        #region ToDouble

        /// <summary>
        ///     转换为双精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static double ToDouble(object data)
        {
            if (data == null)
                return 0;

            return double.TryParse(data.ToString(), out var result) ? result : 0;
        }

        /// <summary>
        ///     转换为双精度浮点数,并按指定的小数位4舍5入
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="digits">小数位数</param>
        public static double ToDouble(object data, int digits)
        {
            return Math.Round(ToDouble(data), digits);
        }

        /// <summary>
        ///     转换为双精度浮点数，小于0返回0，否则返回原double值
        /// </summary>
        /// <param name="data">数据</param>
        public static double ToDouble0(object data)
        {
            var value = ToDouble(data);
            return value < 0 ? 0 : value;
        }

        /// <summary>
        ///     转换为双精度浮点数，小于0返回0，否则返回原double值
        /// </summary>
        /// <param name="data">数据</param>
        public static double ToDouble1(object data)
        {
            var value = ToDouble(data);
            return value < 1 ? 1 : value;
        }

        /// <summary>
        ///     转换为可空双精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static double? ToDoubleOrNull(object data)
        {
            if (data == null)
                return null;

            var isValid = double.TryParse(data.ToString(), out var result);
            if (isValid)
                return result;

            return null;
        }

        #endregion ToDouble

        #region ToDecimal

        /// <summary>
        ///     转换为高精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static decimal ToDecimal(object data)
        {
            if (data == null)
                return 0;

            return decimal.TryParse(data.ToString(), out var result) ? result : 0;
        }

        /// <summary>
        ///     转换为高精度浮点数,并按指定的小数位4舍5入
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="digits">小数位数</param>
        public static decimal ToDecimal(object data, int digits)
        {
            return Math.Round(ToDecimal(data), digits);
        }

        /// <summary>
        ///     转换为高精度浮点数，小于0返回0，否则返回原decimal值
        /// </summary>
        /// <param name="data">数据</param>
        public static decimal ToDecimal0(object data)
        {
            var value = ToDecimal(data);
            return value < 0 ? 0 : value;
        }

        /// <summary>
        ///     转换为高精度浮点数，小于1返回1，否则返回原decimal值
        /// </summary>
        /// <param name="data">数据</param>
        public static decimal ToDecimal1(object data)
        {
            var value = ToDecimal(data);
            return value < 1 ? 1 : value;
        }

        /// <summary>
        ///     转换为可空高精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static decimal? ToDecimalOrNull(object data)
        {
            if (data == null)
                return null;

            var isValid = decimal.TryParse(data.ToString(), out var result);
            if (isValid)
                return result;

            return null;
        }

        /// <summary>
        ///     转换为可空高精度浮点数,并按指定的小数位4舍5入
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="digits">小数位数</param>
        public static decimal? ToDecimalOrNull(object data, int digits)
        {
            var result = ToDecimalOrNull(data);
            if (result == null)
                return null;

            return Math.Round(result.Value, digits);
        }

        #endregion ToDecimal

        #endregion 数值转换

        #region 日期转换

        /// <summary>
        ///     转换为日期
        /// </summary>
        /// <param name="data">数据</param>
        public static DateTime ToDate(object data)
        {
            if (data == null)
                return DateTime.MinValue;

            return DateTime.TryParse(data.ToString(), out var result) ? result : DateTime.MinValue;
        }

        /// <summary>
        ///     转换为可空日期
        /// </summary>
        /// <param name="data">数据</param>
        public static DateTime? ToDateOrNull(object data)
        {
            if (data == null)
                return null;

            var isValid = DateTime.TryParse(data.ToString(), out var result);
            if (isValid)
                return result;

            return null;
        }

        #endregion 日期转换

        #region 布尔转换

        /// <summary>
        ///     转换为布尔值
        /// </summary>
        /// <param name="data">数据</param>
        public static bool ToBool(object data)
        {
            if (data == null)
                return false;

            var value = GetBool(data);
            if (value != null)
                return value.Value;

            return bool.TryParse(data.ToString(), out var result) && result;
        }

        /// <summary>
        ///     获取布尔值
        /// </summary>
        private static bool? GetBool(object data)
        {
            switch (data.ToString()?.Trim().ToLower())
            {
                case "0":
                    return false;
                case "1":
                    return true;

                case "是":
                    return true;

                case "否":
                    return false;

                case "yes":
                    return true;

                case "no":
                    return false;

                default:
                    return null;
            }
        }

        /// <summary>
        ///     转换为可空布尔值
        /// </summary>
        /// <param name="data">数据</param>
        public static bool? ToBoolOrNull(object data)
        {
            if (data == null)
                return null;

            var value = GetBool(data);
            if (value != null)
                return value.Value;

            var isValid = bool.TryParse(data.ToString(), out var result);
            if (isValid)
                return result;

            return null;
        }

        #endregion 布尔转换

        #region 字符串转换

        /// <summary>
        ///     转换为字符串，并去除两边空格
        /// </summary>
        /// <param name="data">数据</param>
        public static string ToString(object data)
        {
            return data == null ? string.Empty : data.ToString()?.Trim();
        }

        #endregion 字符串转换

        #region 通用转换

        public static object ChangeType(object value, Type conversion)
        {
            var t = conversion;
            if (value == null) return null;

            //判断是否为可空类型
            if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                // 获取实际类型
                t = Nullable.GetUnderlyingType(t);
            }
            if (t.IsEnum && int.TryParse(value?.ToString(), out var v))
            {
                return Enum.ToObject(t, v);
            }
            return Convert.ChangeType(value, t);
        }

        /// <summary>
        ///     转换为目标元素
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="data">数据</param>
        public static T To<T>(object data)
        {
            if (data == null)
                return default;
            if (data is string && string.IsNullOrWhiteSpace(data.ToString()))
                return default;
            var type = GetType<T>();
            try
            {
                if (type.Name.ToLower() == "guid")
                    return (T)(object)new Guid(data.ToString() ?? string.Empty);
                if (data is IConvertible)
                    return (T)Convert.ChangeType(data, type);
                return (T)data;
            }
            catch
            {
                return default;
            }
        }

        #region GetType(获取类型)

        /// <summary>
        ///     获取类型,对可空类型进行处理
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        public static Type GetType<T>()
        {
            return Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
        }

        #endregion GetType(获取类型)

        #endregion 通用转换
    }
}
